package com.qsmx.store.core.net;


import com.qsmx.store.core.app.Atmos;
import com.qsmx.store.core.app.ConfigKeys;
import com.qsmx.store.core.fusion.FusionPre;
import com.qsmx.store.core.util.storage.AtmosPreference;

import java.util.ArrayList;
import java.util.WeakHashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * @ClassName: RestCreater
 * @Description: java类作用描述
 * @Author: Jove.Nan
 * @Date: 2019/2/22 1:23 PM
 */
public class RestCreater {

    private static final class ParamsHolder {
        public static final WeakHashMap<String, Object> PARAMS = new WeakHashMap<>();
    }

    public static WeakHashMap<String, Object> getParams() {
        return ParamsHolder.PARAMS;
    }

    private static final class RetrofitHolder {
        private static final String BASE_URL = (String) Atmos.getConfiguration(ConfigKeys.API_HOST);
        private static final String BASE_TEST_URL = (String) Atmos.getConfiguration(ConfigKeys.API_TEST_HOST);

        private static final Retrofit RETROFIT_CLIENT = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .client(OKHttpHolder.OK_HTTP_CLIENT)
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        private static final Retrofit RETROFIT_CLIENT_TEST = new Retrofit.Builder()
                .baseUrl(BASE_TEST_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .client(OKHttpHolder.OK_HTTP_CLIENT)
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    private static final class OKHttpHolder {
        private static final int TIME_OUT = 30;
        private static final OkHttpClient.Builder BUILDER = new OkHttpClient.Builder();
        private static final ArrayList<Interceptor> INTERCEPTORS = Atmos.getConfiguration(ConfigKeys.INTERCEPTOR);

        private static OkHttpClient.Builder addInterceptor() {
            if (INTERCEPTORS != null && !INTERCEPTORS.isEmpty()) {
                for (Interceptor interceptor : INTERCEPTORS) {
                    BUILDER.addInterceptor(interceptor);
                }
            }
            return BUILDER;
        }

        private static final OkHttpClient OK_HTTP_CLIENT = addInterceptor().connectTimeout(TIME_OUT, TimeUnit.SECONDS).readTimeout(TIME_OUT, TimeUnit.SECONDS).callTimeout(TIME_OUT, TimeUnit.SECONDS).writeTimeout(TIME_OUT, TimeUnit.SECONDS).build();
    }

    private static final class RestServiceHolder {
        private static final RestService REST_SERVICE = RetrofitHolder.RETROFIT_CLIENT.create(RestService.class);

        private static final RestService REST_SERVICE_TEST = RetrofitHolder.RETROFIT_CLIENT_TEST.create(RestService.class);
    }


    public static RestService getRestService() {
        if (AtmosPreference.getCustomStringPre(FusionPre.APP_ELM_INFO).equals("测试")) {
            return RestServiceHolder.REST_SERVICE_TEST;
        } else {
            return RestServiceHolder.REST_SERVICE;
        }
    }


//    private static final class RxRestServiceHolder {
//        private static final RxRestService REST_SERVICE = RetrofitHolder.RETROFIT_CLIENT.create(RxRestService.class);
//    }
//
//    public static RxRestService getRxRestService() {
//        return RxRestServiceHolder.REST_SERVICE;
//    }


}
